﻿#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setFixedSize(this->size());//锁定窗口大小
    this->setCentralWidget(ui->stackedWidget);//设置中心组件

    iniFoodList();//初始化菜品列表

    iniUi();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::iniFoodList()
{
    m_cpupon = false;//默认不送优惠券

    //打开菜品文件
    QFile file;
    file.setFileName("food.txt");
    if(file.open(QIODevice::ReadOnly) == false){//如果打开失败则发出警告
        QMessageBox::warning(0,"警告","菜品信息初始化失败,请检查菜品文件.");
        return;
    }

    QTextStream stream(&file);//定义文件文本读取流
    if(file.isOpen()){
        QString tempStr;//定义中间字符串,用于保存每行读取的数据流
        QStringList tempList;//定义中间字符串列表用于分解中间字符串,通过空格拆分
        Food* food;//定义中间菜品类指针
        int id;//菜品id
        QString name;//菜品名称
        double price;//菜品价格
        QString introduce;//菜品介绍
        QString picPath;//菜品图片路径
        for(;!stream.atEnd();){
            tempStr = stream.readLine();//读取一行

            //处理中间字符串
            tempList = tempStr.simplified().trimmed().split(' ');//处理中间字符串前会先合并空格并去掉首尾空格

            //为对应的中间变量赋值
            id = tempList.at(0).toInt();
            name = tempList.at(1);
            price = tempList.at(2).toDouble();
            introduce = tempList.at(3);
            picPath = tempList.at(4);

            //创建菜品类
            food = new Food(id,name,price,introduce,picPath);

            //将菜品类加入菜品列表
            m_foodList.append(food);

        }

        file.close();//关闭文件
    }
}

void MainWindow::iniUi()
{
    ui->groupOpe->setEnabled(false);//初始化优惠操作不可用
    ui->btnSave->setEnabled(false);//初始化保存操作不可用

    ui->tvFoodList->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//拉伸表格
    ui->tvChoice->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//拉伸表格
    m_foodModel.setColumnCount(3);//设置表格列数
    m_choiceModel.setColumnCount(3);

    //设置表格表头
    m_foodModel.setHeaderData(0,Qt::Horizontal,"编号");
    m_foodModel.setHeaderData(1,Qt::Horizontal,"名称");
    m_foodModel.setHeaderData(2,Qt::Horizontal,"价格");

    m_choiceModel.setHeaderData(0,Qt::Horizontal,"编号");
    m_choiceModel.setHeaderData(1,Qt::Horizontal,"名称");
    m_choiceModel.setHeaderData(2,Qt::Horizontal,"价格");

    //将菜品列表中的信息载入到表格中
    QList<QStandardItem*> items;
    int count = m_foodList.count();
    for(int i = 0;i < count;++i){
        items.clear();

        QStandardItem* item;
        item = new QStandardItem((QString::asprintf("%d",m_foodList.at(i)->getId())));
        items.append(item);

        item = new QStandardItem(m_foodList.at(i)->getName());
        items.append(item);

        item = new QStandardItem((QString::asprintf("%.2f",m_foodList.at(i)->getPrice())));
        items.append(item);

        m_foodModel.appendRow(items);
    }

    //绑定表格和模型
    ui->tvFoodList->setModel(&m_foodModel);
    ui->tvChoice->setModel(&m_choiceModel);
}


void MainWindow::on_tvFoodList_clicked(const QModelIndex &index)
{
    if(ui->tvFoodList->currentIndex().isValid()){//如果当前选择列可用则更新介绍和图片信息
        ui->textIntroduce->setText(m_foodList.at(index.row())->getIntroduce());//设置介绍信息
        ui->labPic->setPixmap(QPixmap(m_foodList.at(index.row())->getPicPath()).scaled(ui->labPic->size()));//设置图片
    }
}


void MainWindow::on_btnChoice_clicked()
{
    //将选择的菜品同步到已选择表格中
    if(ui->tvFoodList->currentIndex().isValid()){

        //初始化选择的行信息
        QList<QStandardItem*> items;
        QStandardItem* item;

        item = new QStandardItem(QString::asprintf("%d",m_foodList.at(ui->tvFoodList->currentIndex().row())->getId()));
        items.append(item);

        item = new QStandardItem(m_foodList.at(ui->tvFoodList->currentIndex().row())->getName());
        items.append(item);

        item = new QStandardItem(QString::asprintf("%.2f",m_foodList.at(ui->tvFoodList->currentIndex().row())->getPrice()));
        items.append(item);

        m_choiceModel.appendRow(items);//添加行信息

        //计算总价
        double total = 0;
        for(int i = 0;i < m_choiceModel.rowCount();++i){
            total += m_choiceModel.data(m_choiceModel.index(i,2)).toDouble();
        }
        ui->editTotalPrice->setText(QString::asprintf("%.2f",total));//更新界面信息

        ui->groupOpe->setEnabled(true);//设置优惠操作可用
        ui->btnSave->setEnabled(true);//设置保存操作可用
    }
}


void MainWindow::on_btnCancel_clicked()
{
    if(ui->tvChoice->currentIndex().isValid()){//如果当前选择可用则移除当前行
        m_choiceModel.removeRow(ui->tvChoice->currentIndex().row());

        //计算总价
        double total = 0;
        for(int i = 0;i < m_choiceModel.rowCount();++i){
            total += m_choiceModel.data(m_choiceModel.index(i,2)).toDouble();
        }
        ui->editTotalPrice->setText(QString::asprintf("%.2f",total));//更新界面信息

        if(m_choiceModel.rowCount() == 0){//如果选择的菜品为空
            m_cpupon = false;//设置不赠送优惠券
            ui->groupOpe->setEnabled(false);//初始化优惠操作不可用
            ui->btnSave->setEnabled(false);//初始化保存操作不可用
        }
    }
}


void MainWindow::on_btn1_clicked()
{
    //根据选中的打折倍率不同进行不同的折扣
    double total = ui->editTotalPrice->text().toDouble();//获取总价
    if(ui->comb1->currentIndex() == 0){
        ui->editTotalPrice->setText(QString::asprintf("%.2f",total*0.9));
    }else if(ui->comb1->currentIndex() == 1){
        ui->editTotalPrice->setText(QString::asprintf("%.2f",total*0.7));
    }else if(ui->comb1->currentIndex() == 2){
        ui->editTotalPrice->setText(QString::asprintf("%.2f",total*0.5));
    }
}


void MainWindow::on_btn2_clicked()
{

    //满减,可叠加
    double total = ui->editTotalPrice->text().toDouble();//获取总价
    double temp = total;//满减后的价格
    if(total >= 199){
        temp = temp - 10;
    }
    if(total >= 299){
        temp -= 25;
    }
    if(total >= 399){
        temp -= 45;
    }

    ui->editTotalPrice->setText(QString::asprintf("%.2f",temp));//设置满减后的费用
}


void MainWindow::on_btn3_clicked()
{
    QMessageBox::information(0,"提示","操作成功,稍后将打印在保存文件中!");
    m_cpupon = true;
}


void MainWindow::on_btnSave_clicked()
{

    //写入订单信息
    QString path = "";
    path = QFileDialog::getSaveFileName(0,"选择保存位置",QDir::homePath(),"文本文件(*.txt);;任意文件(*.*)");
    if(!path.isEmpty()){
        QFile file(path);
        file.open(QIODevice::WriteOnly|QIODevice::Text);
        if(!file.isOpen()){
            QMessageBox::warning(0,"警告","文件保存失败!");
            return;
        }

        file.write("编号 名称 价格\n");
        for(int i = 0;i < m_choiceModel.rowCount();++i){
            file.write(m_choiceModel.data(m_choiceModel.index(i,0)).toString().toUtf8());
            file.write(" ");
            file.write(m_choiceModel.data(m_choiceModel.index(i,1)).toString().toUtf8());
            file.write(" ");
            file.write(m_choiceModel.data(m_choiceModel.index(i,2)).toString().toUtf8());
            file.write("\n");

        }

        //计算总价
        double total = 0;
        for(int i = 0;i < m_choiceModel.rowCount();++i){
            total += m_choiceModel.data(m_choiceModel.index(i,2)).toDouble();
        }

        file.write("总价(优惠后):"+ui->editTotalPrice->text().toUtf8()+"  优惠前:"+QString::asprintf("%.2f",total).toUtf8()+"  座位号:"+ui->editSeat->text().toUtf8()+"\n");

        if(m_cpupon){//赠送优惠劵
            file.write("赠送优惠劵:");
            if(total < 299){
                file.write("无.");
            }else if(total >= 299 && total < 399){
                file.write("9折优惠券.");
            }else if(total >= 399 && total < 499){
                file.write("8.8折优惠券.");
            }else if(total >= 499){
                file.write("8.5折优惠券.");
            }
        }
        file.close();//关闭文件
    }
}

